import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from "@angular/common/http";
import { Observable, Subject } from "rxjs";
import {Consistent} from "../entity/Consistent"
import { CCSLSet } from '../entity/CCSLSet';
import { SMTRes } from '../entity/SMTRes';
import { minunsatRes } from '../entity/minunsatRes';
@Injectable({
  providedIn: 'root'
})
export class SafenlService {

  constructor(
    private http: HttpClient
  ) { }

  projectName: string;
  safeNL: string;
  ccsls: CCSLSet[];

  httpOptions = {
		headers: new HttpHeaders({ 'Content-Type': 'application/json' })
	};

  private ccslEmit = new Subject<any>();
  ccslEmmited$ = this.ccslEmit.asObservable();
  getCCSL(ccsls: CCSLSet[]) {
    this.ccslEmit.next(ccsls);
  }

  private isCCSLEmit = new Subject<any>();
  isCCSLEmmited$ = this.isCCSLEmit.asObservable();
  isCCSL(isCCSL: boolean) {
    this.isCCSLEmit.next(isCCSL);
  }

  private CGEmit = new Subject<any>();
  CGEmmited$ = this.CGEmit.asObservable();
  setCG(CGs: string[]) {
    this.CGEmit.next(CGs);
  }

  private CCGEmit = new Subject<any>();
  CCGEmmited$ = this.CCGEmit.asObservable();
  setCCG(CCGs: string[]) {
    this.CCGEmit.next(CCGs);
  }

  private RCGEmit = new Subject<any>();
  RCGEmmited$ = this.RCGEmit.asObservable();
  setRCG(RCGs: string[]) {
    this.RCGEmit.next(RCGs);
  }

  private CircleEmit = new Subject<any>();
  CircleEmmited$ = this.CircleEmit.asObservable();
  setCircle(Circles: string[]) {
    this.CircleEmit.next(Circles);
  }

  private RECCGEmit = new Subject<any>();
  RECCGEmmited$ = this.RECCGEmit.asObservable();
  setRECCG(RECCGs: string[]) {
    this.RECCGEmit.next(RECCGs);
  }

  private ConsistentEmit = new Subject<any>();
  ConsistentEmmited$ = this.ConsistentEmit.asObservable();
  setConsistent(Consistents: Consistent[]) {
    this.ConsistentEmit.next(Consistents);
  }

  private LayerEmit = new Subject<any>();
  LayerEmmited$ = this.LayerEmit.asObservable();
  setLayer(Layer: string) {
    this.LayerEmit.next(Layer);
    console.log(Layer);
  }

  private SMTEmit = new Subject<any>();
  SMTEmmited$ = this.SMTEmit.asObservable();
  setSMTRes(SMTRes: SMTRes[]) {
    this.SMTEmit.next(SMTRes);
  }

  
  private consistEmit = new Subject<any>();
  consistEmit$ = this.consistEmit.asObservable();
  setConsistRes(consistRes: minunsatRes){
    this.consistEmit.next(consistRes)
  }

  toCCSL(userName: string, projectName: string, version: string, stateFileName: string){
    const url = `http://47.52.116.116:8075/safeNL/toCCSL?userName=${userName}&projectName=${projectName}&version=${version}&stateFile=${stateFileName}`;
    var ccsls = this.http.get<CCSLSet[]>(url);
    return ccsls;
  }

  safenlCheck(userName: string, projectName: string, version: string){
    const url = `http://47.52.116.116:8075/safeNL/safeNLCheck?userName=${userName}&projectName=${projectName}&version=${version}`;
    var safenlList = this.http.get<any>(url);
    return safenlList;
  }

  getCG(userName: string, projectName: string, version: string){
    this.projectName = projectName
    const url = `http://47.52.116.116:8075/ccsl/toCG?userName=${userName}&projectName=${projectName}&version=${version}`;
    var res = this.http.get<number>(url);
    return res;
  }

  getCCG(fileName: string){
    this.projectName = fileName
    const url = `http://47.52.116.116:8075/ccsl/toCCG?fileName=${fileName}`;
    var res = this.http.get<boolean>(url);
    return res;
  }

  getCircle(fileName: string){
    const url = `http://47.52.116.116:8075/ccsl/checkCircle?fileName=${fileName}`;
    var res = this.http.get<string[]>(url);
    return res;
  }

  getRECCG(fileName: string){
    const url = `http://47.52.116.116:8075/ccsl/getRECCG?fileName=${fileName}`;
    var RECCGs = this.http.get<string[]>(url);
    return RECCGs;
  }

  ccslCheck(userName: string, projectName: string, version: string){
    console.log(version)
    this.projectName = projectName;
    const url = `http://47.52.116.116:8075/ccsl/getConsistent?userName=${userName}&projectName=${projectName}&version=${version}`;
    var Consistents = this.http.get<Consistent[]>(url);
    return Consistents;
  }

  SMTCheck(userName: string, projectName: string, version: string, timeout: number, bound: number,  period: boolean, pv: number, deadlock: boolean){
    const url = `http://47.52.116.116:8075/smt/z3Check?userName=${userName}&projectName=${projectName}&version=${version}&timeout=${timeout}&bound=${bound}&period=${period}&pv=${pv}&deadlock=${deadlock}`;
    console.log(url)
    var res = this.http.get<SMTRes[]>(url);
    return res;
  }

  minUnsat_smt(userName: string, projectName: string, version: string){
    this.projectName = projectName;
    const url = `http://47.52.116.116:8075/unsat/minunsat`;
    var res = this.http.get<string>(url);
    return res;
  }

  RTCheck(userName: string, projectName: string, version: string){
    console.log(version)
    this.projectName = projectName;
    const url = `http://47.52.116.116:8075/ccsl/checkRT?userName=${userName}&projectName=${projectName}&version=${version}`;
    var Consistents = this.http.get<Consistent[]>(url);
    return Consistents;
  }
  // displayCG(fileName: string){
  //   const url = `http://localhost:8080/ccsl/display?fileName=${fileName}`;
  //   var res = this.http.get<boolean>(url);
  //   return res;
  // }
}
